package com.googlecode.jascii;

import java.util.ArrayList;

import javax.script.Invocable;

/**
 * @author Kim Lee
 * TrResult負責一列的設定,並且當TD需要換行,自動新增TR範本
 * 使用範例 請參照TrResult.main 
 */

public class TrResult {
//	private static final String leftTop  ="┌";
//	private static final String leftMed  ="├";
//	private static final String leftDown ="└";
//	private static final String col ="│";
//	private static final String rightTop  ="┐";
//	private static final String rightMed  ="┤";
//	private static final String rightDown ="┘";
//	private static final String row ="─";
//	private static final String aross ="┼";
//	public static final int TR_DATA_TYPE = 1;
//	public static final int TR_ASCII_TYPE = 2;
//	public static final int TR_COLUMN_TYPE = 3;
	public static final int TD_TEXT_TYPE = 0;   // 000 000
	public static final int TD_SCRIPT_TYPE = 8; // 001 000
	private int trType = TD_TEXT_TYPE;//1:第一個/2:中間 /3:最後
	private int width=0;
	private int tdNum=0;
	private int height=1;
    private ArrayList tdArray = new ArrayList();
    private boolean nextPage=false;
    private Invocable invocableEngine  = null;
	public TrResult() {
		super();
	}
	
    public TrResult append(TdResult tdResult)
    {
    	if(tdResult instanceof TdShellResult)
    	{
    		this.trType = TD_SCRIPT_TYPE;
    	}
    	width +=tdResult.getDefaultLength();
    	tdNum++;
    	tdArray.add(tdResult);
    	if(height < tdResult.getHeight()) height = tdResult.getHeight();
    	return this;
    }
    /**
	 * @return 傳回 height。
	 */
	public int getHeight() {
		return height;
	}
	
	public ArrayList getTdArray() {
		return tdArray;
	}
    public TrResult getTrResult(int i)
    {
    	if(height!=1)
    	{
    		TrResult trtemp = new TrResult();
    		for(int index=0 ; index<tdArray.size();index++)
    		{
    			TdResult td = (TdResult) tdArray.get(index);
    			TdResult tdtemp = td.getTdResult(i);
    			if(tdtemp==null && i != tdArray.size())
    			{
    				if(td.getDataType()==TdResult.TD_ASCII_TYPE)
    				tdtemp =  new TdResult(td.getDefaultStr(),td.getDefaultLength(), td.getStyle()|td.getDataType() ,td.isNotTrim() );
    				else 
    					tdtemp =  new TdResult(td.getValue(),td.getDefaultLength(), td.getStyle()|td.getDataType() ,td.isNotTrim());
    				
    			}
    			if(tdtemp!=null)trtemp.append(tdtemp);
    		}
    		return trtemp;
    		
    	}else return this;
    }
    
    
    public String toString()
    {
    	StringBuffer sb = new StringBuffer();
    	for(int i=0 ;i<tdArray.size();i++)
    	{
    		TdResult td = (TdResult)tdArray.get(i);
    		if(td!=null)
    		{
    			if(trType == TD_TEXT_TYPE)
    				sb.append(td);
    			else if(trType == TD_SCRIPT_TYPE)
    			{
    				if(td instanceof TdShellResult && invocableEngine!=null)
    				{
    					TdShellResult tdShell = (TdShellResult)td;
    					tdShell.invokingFunction(invocableEngine);
    					sb.append(td);
    				}else sb.append(td);
    			}else sb.append(td);
    		}
    	}
    	return sb.toString();
    }
    
    
	public static void main(String[] args) {
		
		TdResult td = new TdResult("中12345678901234567890",17,TdResult.TD_STYLE_MIDDLE);
		//TdResult td1 = new TdResult("123中文1",17,TdResult.TD_STYLE_LEFT);
		//TdResult td2 = new TdResult("123中文1",17,TdResult.TD_STYLE_RIGHT);
		
		TrResult tr = new TrResult();
		tr.append(td);//.append(td1).append(td2);
		System.out.println(tr);
	}
	
	/**
	 * @return Returns the nextPage.
	 */
	public boolean isNextPage() {
		return nextPage;
	}
	/**
	 * @param nextPage The nextPage to set.
	 */
	public void setNextPage(boolean nextPage) {
		this.nextPage = nextPage;
	}
	
	/**
	 * @return Returns the trType.
	 */
	public int getTrType() {
		return trType;
	}
//	/**
//	 * @param trType The trType to set.
//	 */
//	public void setTrType(int trType) {
//		this.trType = trType;
//	}
	/**
	 * @return Returns the width.
	 */
	public int getWidth() {
		return width;
	}

	public void setInvocableEngine(Invocable invocableEngine) {
		this.invocableEngine = invocableEngine;
	}
}
